package com.qfedu.product.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qfedu.product.dao.SellDao;
import com.qfedu.product.po.Check;
import com.qfedu.product.po.SelectVip;
import com.qfedu.product.po.Sum_sell;
import com.qfedu.user.util.DBUtil;

public class sellDaoimpl implements SellDao {

	private DBUtil DBUtil = new DBUtil();
	private ResultSet rs = null;
	private int intResult = -1;
	private boolean boolResult = false;

	@Override
	public Boolean doSell(Object[] objects) {
		String sql = "insert into sell_record(PRODUCTION_ID,PRODUCTION_TYPE,PRODUCTION_NAME,PRODUCTION_NUM,PRODUCTION_PRICE,PHONE_NUM,PRODUCTION_SUM_AFTER,USER_ID) values(?,?,?,?,?,?,?,?) ";
		System.out.println(sql);
		intResult = DBUtil.executeUpdate(sql, objects);
		if (intResult != -1) {
			sql = "select PRODUCTION_NUM-" + objects[3] + " as PRODUCTION_NUM from inventory  where PRODUCTION_ID = '"
					+ objects[3] + "'";
			rs = DBUtil.executeQuery(sql, null);
			try {
				// int a = rs.getInt("PRODUCTION_NUM");
				int a = 0;
				while (rs.next()) {
					a = rs.getInt("PRODUCTION_NUM");
				}
				sql = "update inventory set PRODUCTION_NUM = " + a + "  where PRODUCTION_ID = '" + objects[0] + "'";
				System.out.println(sql);
				intResult = DBUtil.executeUpdate(sql, null);
				if (intResult != -1) {
					return true;
				} else {
					sql = "update inventory set status = 'false' where PRODUCTION_ID = " + objects[0];
					System.out.println(sql);
					intResult = DBUtil.executeUpdate(sql, null);
					return false;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}

		} else {
			return false;
		}
	}

	@Override
	public Check docheck(Object[] objects) {
		Check check = new Check();
		
		String sql = "select b.production_type,b.production_name,a.production_price from production_info a ,inventory b where a.production_id = b.production_id and a.production_id = ?";
		rs = DBUtil.executeQuery(sql, objects);
		try {
			while (rs.next()) {
				check = new Check(rs.getInt(3), rs.getString(1), rs.getString(2));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			check = null;
		}
		return check;
	}

	@Override
	public Boolean doVip(Object[] objects) {
		// TODO Auto-generated method stub
		SelectVip selectVip = new SelectVip();
		String sql = "select * from vipclient where phone_num = ? ";
		rs = DBUtil.executeQuery(sql, objects);
		if (rs==null) {
			return false;
		}else {
			return true ;
		}
		
	}

	public Sum_sell dosum(Object[] objects) {
		Sum_sell sum_sell = new Sum_sell();
		String sql = "select PRODUCTION_PRICE from production_info where PRODUCTION_ID = ?";
		rs = DBUtil.executeQuery(sql, objects);
		try {
			while(rs.next()) {
				sum_sell = new Sum_sell(rs.getInt(3));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sum_sell;
		
		
	}

}
